package com.yjq.blog.vo;

import com.yjq.blog.enums.ErrorCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class Response<T> implements Serializable {

    private Integer code;
    private T data;
    private String message;
    private HostInfo hostInfo;

    private Response() {}

    public static <T> Response<T> success(T data) {
        return new Response().setCode(0).setData(data);
    }

    public static <T> Response<T> fail(ErrorCodeEnum errorCodeEnum, Object data) {
        return new Response().setCode(errorCodeEnum.getCode()).setMessage(errorCodeEnum.getMessage()).setData(data);
    }

    public static <T> Response<T> fail(ErrorCodeEnum errorCodeEnum) {
        return new Response().setCode(errorCodeEnum.getCode()).setMessage(errorCodeEnum.getMessage());
    }

    public static <T> Response<T> fail(Integer code, String message) {
        return new Response().setCode(code).setMessage(message);
    }

    private Response<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    private Response<T> setData(T data) {
        this.data = data;
        return this;
    }

    private Response<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public Response<T> setHostInfo(HostInfo hostInfo) {
        this.hostInfo = hostInfo;
        return this;
    }

}
